﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class ColaFinalInspectionRecordController : ApiController
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] ColaFinalInspectionRecord CFIR)
        {
            ReturnObject RO = new ReturnObject();
            if (CFIR == null ||
                string.IsNullOrEmpty(CFIR.CFIR_Inspector) ||
                string.IsNullOrEmpty(CFIR.CFIR_Shift) ||
                string.IsNullOrEmpty(CFIR.CFIR_LineParting) ||
                string.IsNullOrEmpty(CFIR.CFIR_OrderNumber) ||
                string.IsNullOrEmpty(CFIR.CFIR_ProductModel))
            {
                Logger.Warn("输入值无效");
                RO.Status = HttpStatusCode.BadRequest;
                RO.Message = "输入值无效";
                RO.Data = CFIR;
                return Request.CreateResponse(HttpStatusCode.BadRequest, RO);
            }

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        var query = @"INSERT INTO ColaFinalInspectionRecord 
                                    (CFIRInspector, CFIRShift, CFIRInspectionTime, CFIRLineParting, CFIROrderNumber, 
                                     CFIRProductModel, CFIRBatchQuantity, CFIRSampleQuantity, CFIRInspectionItem, 
                                     CFIRC1DisconnectionDefect, CFIRC2ProtectionGateDefect, CFIRC3HandFeelDefect, 
                                     CFIRC4AppearanceDefect, CFIRC5PadPrintingLaserDefect, CFIRC6AssemblyDefect, 
                                     CFIRC7EquipmentJigDefect, CFIRC8MarkingDefect, CFIRC9InjectionMoldedPartDefect, 
                                     CFIRC10PackagingDefect, CFIRC11VoltageResistanceDefect, CFIRC12OtherDefects, 
                                     CFIRInspectionConclusion, CFIRWhichInspectionQualified, CFIRNonconformanceDescription1,
                                     CFIRNonconformanceImage1,CFIRNonconformanceDescription2,CFIRNonconformanceImage2,
                                     CFIRNonconformanceDescription3,CFIRNonconformanceImage3,CFIRCreateCreator,CFIRCreateTime)
                                    VALUES 
                                    (@Inspector, @Shift, @InspectionTime, @LineParting, @OrderNumber, 
                                     @ProductModel, @BatchQuantity, @SampleQuantity, @InspectionItem, 
                                     @C1DisconnectionDefect, @C2ProtectionGateDefect, @C3HandFeelDefect, 
                                     @C4AppearanceDefect, @C5PadPrintingLaserDefect, @C6AssemblyDefect, 
                                     @C7EquipmentJigDefect, @C8MarkingDefect, @C9InjectionMoldedPartDefect, 
                                     @C10PackagingDefect, @C11VoltageResistanceDefect, @C12OtherDefects, 
                                     @InspectionConclusion, @WhichInspectionQualified, 
                                     @NonconformanceDescription1, @NonconformanceImage1, 
                                     @NonconformanceDescription2, @NonconformanceImage2, 
                                     @NonconformanceDescription3, @NonconformanceImage3,@CreateCreator,@CreateTime);
                                    SELECT CAST(scope_identity() AS int);";

                        SqlParameter[] parameters = {
                            new SqlParameter("@Inspector", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_Inspector },
                            new SqlParameter("@Shift", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_Shift },
                            new SqlParameter("@InspectionTime", SqlDbType.DateTime) { Value = CFIR.CFIR_InspectionTime },
                            new SqlParameter("@LineParting", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_LineParting },
                            new SqlParameter("@OrderNumber", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_OrderNumber },
                            new SqlParameter("@ProductModel", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_ProductModel },
                            new SqlParameter("@BatchQuantity", SqlDbType.Int) { Value = CFIR.CFIR_BatchQuantity },
                            new SqlParameter("@SampleQuantity", SqlDbType.Int) { Value = CFIR.CFIR_SampleQuantity },
                            new SqlParameter("@InspectionItem", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_InspectionItem },
                            new SqlParameter("@C1DisconnectionDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C1DisconnectionDefect ?? DBNull.Value },
                            new SqlParameter("@C2ProtectionGateDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C2ProtectionGateDefect ?? DBNull.Value },
                            new SqlParameter("@C3HandFeelDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C3HandFeelDefect ?? DBNull.Value },
                            new SqlParameter("@C4AppearanceDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C4AppearanceDefect ?? DBNull.Value },
                            new SqlParameter("@C5PadPrintingLaserDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C5PadPrintingLaserDefect ?? DBNull.Value },
                            new SqlParameter("@C6AssemblyDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C6AssemblyDefect ?? DBNull.Value },
                            new SqlParameter("@C7EquipmentJigDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C7EquipmentJigDefect ?? DBNull.Value },
                            new SqlParameter("@C8MarkingDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C8MarkingDefect ?? DBNull.Value },
                            new SqlParameter("@C9InjectionMoldedPartDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C9InjectionMoldedPartDefect ?? DBNull.Value },
                            new SqlParameter("@C10PackagingDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C10PackagingDefect ?? DBNull.Value },
                            new SqlParameter("@C11VoltageResistanceDefect", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C11VoltageResistanceDefect ?? DBNull.Value },
                            new SqlParameter("@C12OtherDefects", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_C12OtherDefects ?? DBNull.Value },
                            new SqlParameter("@InspectionConclusion", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_InspectionConclusion },
                            new SqlParameter("@WhichInspectionQualified", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_WhichInspectionQualified },
                            new SqlParameter("@NonconformanceDescription1", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceDescription1 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage1", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceImage1 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceDescription2", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceDescription2 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage2", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceImage2 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceDescription3", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceDescription3 ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage3", SqlDbType.NVarChar, 500) { Value = (object)CFIR.CFIR_NonconformanceImage3 ?? DBNull.Value },
                            new SqlParameter("@CreateCreator", SqlDbType.NVarChar, 50) { Value = CFIR.CFIR_CreateCreator },
                            new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now }
                        };

                        using (var command = new SqlCommand(query, connection, transaction))
                        {
                            command.Parameters.AddRange(parameters);
                            var result = await command.ExecuteScalarAsync();
                            if (result != null)
                            {
                                transaction.Commit();
                                CFIR.CFIR_ID = Convert.ToInt32(result);
                                Logger.Info("新增成功");
                                RO.Status = HttpStatusCode.OK;
                                RO.Message = "OK";
                                RO.Data = CFIR;
                                return Request.CreateResponse(HttpStatusCode.OK, RO);
                            }
                            else
                            {
                                transaction.Rollback();
                                Logger.Error("插入记录失败");
                                RO.Status = HttpStatusCode.InternalServerError;
                                RO.Message = "Error";
                                RO.Data = CFIR;
                                return Request.CreateResponse(HttpStatusCode.InternalServerError, "插入记录失败");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Warn("新增失败: " + ex.Message);
                        RO.Status = HttpStatusCode.InternalServerError;
                        RO.Message = "Error" + ex.Message;
                        RO.Data = CFIR;
                        return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                    }
                }
            }
        }
    }
}